package com.RyanRestaurant.Controller;

import com.RyanRestaurant.Entity.Desk;
import com.RyanRestaurant.Entity.Food;
import com.RyanRestaurant.Entity.FoodType;
import com.RyanRestaurant.Service.DeskService;
import com.RyanRestaurant.Service.FoodService;
import com.RyanRestaurant.Service.FoodTypeService;
import com.RyanRestaurant.Utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * menu的表示层
 */

@Controller
@Scope("prototype") //spring注解的bean可以变成多例
public class MenuController {

    @Autowired
    private DeskService deskService;

    @Autowired
    private FoodTypeService foodTypeService;

    @Autowired
    private FoodService foodService;


    /**
     * 进入点餐页面
     */
    @GetMapping("/menu_UI/{desk_id}/{type_id}/{currentPage}")
    public String menu_UI(@PathVariable("desk_id") Integer desk_id, @PathVariable("type_id") Integer type_id, @PathVariable("currentPage") Integer currentPage, ModelMap map)
    {
        //当前餐桌
        Desk desk = deskService.getDeskByID(desk_id);
        map.addAttribute("desk", desk);

        //所有的可用菜系
        List<FoodType> foodTypes = foodTypeService.getAllFoodType();
        map.addAttribute("foodTypes", foodTypes);

        //条件查询
        Map<String, Object> condition = new HashMap<>();
        condition.put("type_id", type_id);
        condition.put("currentPage", (currentPage - 1) * 6);
        condition.put("pageSize", 6);

        int totalCount = foodService.menuTotalCount(condition);
        List<Food> foods = foodService.menuGetFoods(condition);

        //封装成工具类
        PageUtils<Food> pageUtils = new PageUtils<>(6, currentPage, totalCount, foods);
        map.addAttribute("pageUtils", pageUtils);
        map.addAttribute("type_id", type_id);
        System.out.println(pageUtils);

        return "menu";
    }

    /**
     * 跳转到菜品的详情
     */
    @GetMapping("/menuToInfo/{foodID}/{deskID}/{type_id}")
    public String menuToInfo(@PathVariable("foodID") Integer foodID, @PathVariable("deskID") Integer deskID, @PathVariable("type_id") Integer type_id, ModelMap map)
    {
        //1.查询菜品的详情
        Food food = foodService.getFoodById(foodID);
        map.addAttribute("food", food);

        //2.传递餐桌
        Desk desk = deskService.getDeskByID(deskID);
        map.addAttribute("desk", desk);

        //3.获取所有的可用菜系
        List<FoodType> foodTypes = foodTypeService.getAllFoodType();
        map.addAttribute("foodTypes", foodTypes);
        map.addAttribute("type_id", type_id);

        //2.跳转
        return "foodInfo";
    }
}
